import { Session } from './session';
import axios from 'axios'
import qs from 'qs'

// 获取所有数据，使用缓存
export async function getListNew(url, params = null, useCache = true) {
  const cacheKey = getFormatFullUrl(url, params)  // getCacheKeyByUrlAndParam(urlSet, params);
  const dataFromCache = Session.get(cacheKey);
  let listData = null
  if (useCache && dataFromCache) { //如果确定使用缓存
    listData = dataFromCache.listData
  }
  let hasData = false
  if (!listData) {//缓存中没有，则去服务器
    if (!params) {
      params = {}
    }

    const result = await axios.get(url, {
      params, paramsSerializer: {
        serialize: params => {
          return qs.stringify(params, { indices: false })
        }
      }
    })
    //  await request.get<T>(urlSet.apiUrl,
    //     { params }
    // );
    // listData = { list: result.data, totalCount: result.totalCount }
    const resultData = result.data
    let totalCount = 0
    if (resultData) {
      if (resultData.listData) {
        listData = resultData.listData
        totalCount = resultData.totalCount
      } else {
        listData = resultData
        totalCount = listData.length
      }

    }


    hasData = listData && listData.length > 0
    if (useCache && hasData) {//拥有数据的，才进行缓存,

      Session.set(cacheKey, { listData, totalCount });
    }
  } else {
    hasData = listData && listData.length > 0
  }

  if (hasData && params && params.searchText && params.searchFieldNames) {
    const searchFields = params.searchFieldNames
    const sText = params.searchText
    const filterData = listData.filter((dataItem) => {  //list.
      let r = false
      for (let i = 0; i < searchFields.length; i++) {
        const fname = searchFields[i]
        if (dataItem[fname] && dataItem[fname].toString().indexOf(sText) != -1) {
          r = true;
        }
      }
      return r;
    })

    listData = filterData // .list= { list: filterData, totalCount: dataFromCache.totalCount }
  }

  return listData

}

export async function getList(url, params = null, useCache = true) {
  const cacheKey = getFormatFullUrl(url, params)  // getCacheKeyByUrlAndParam(urlSet, params);

  let listData = null
  if (useCache) { //如果确定使用缓存
    listData = Session.get(cacheKey);
  }
  let hasData = false
  if (!listData) {//缓存中没有，则去服务器
    if (!params) {
      params = {}
    }
    const result = await axios.get(url, {
      params, paramsSerializer: params => {
        return qs.stringify(params, { indices: false })
      }
    })
    //  await request.get<T>(urlSet.apiUrl,
    //     { params }
    // );
    // listData = { list: result.data, totalCount: result.totalCount }
    let resultData = result.data
    if(resultData){
      if(resultData.list){
        listData = resultData.list
      }else
        listData = resultData
    }
    //listData = (result?.data?.list && result?.data?.totalCount) ? result?.data?.list : result.data
    hasData = listData && listData.length > 0
    if (useCache && hasData) {//拥有数据的，才进行缓存,
      Session.set(cacheKey, listData);
    }
  } else {
    hasData = listData && listData.length > 0
  }

  if (hasData && params && params.searchText && params.searchFieldNames) {
    const searchFields = params.searchFieldNames
    const sText = params.searchText
    const filterData = listData.filter((dataItem) => {  //list.
      let r = false
      for (let i = 0; i < searchFields.length; i++) {
        const fname = searchFields[i]
        if (dataItem[fname] && dataItem[fname].toString().indexOf(sText) != -1) {
          r = true;
        }
      }
      return r;
    })

    listData = filterData // .list= { list: filterData, totalCount: dataFromCache.totalCount }
  }

  return listData

}


export function getFormatFullUrl(url, data) {
  let params = ``;
  for (let key in data) {
    if (data[key] != null && data[key] != '') {
      if (params.length == 0) {
        params += `?${key}=${data[key]}`;
      } else {
        params += `&${key}=${data[key]}`;
      }
    }
  }
  url += params;
  return url;
}